package com.xjc.business.controller.schadmin;

import com.xjc.business.entity.po.schadmin.AllocaDormitoryPo;
import com.xjc.business.entity.po.schadmin.BuildingPo;
import com.xjc.business.entity.vo.schadmin.BuildingDetailsVo;
import com.xjc.business.entity.vo.schadmin.BuildingVo;
import com.xjc.business.entity.vo.schadmin.RestBedDetails;
import com.xjc.business.service.BuildingsService;
import com.xjc.system.constants.MyConstants;
import com.xjc.system.exception.MyException;
import com.xjc.system.result.Result;
import com.xjc.system.result.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 徐锦程
 * 时间:2025-01-28 11:42
 */
@RestController
@RequestMapping("/sch_admin/building")
public class BuildingController {

    @Autowired
    BuildingsService buildingsService;

    /**
     * 获取楼宇列表
     * @param schoolId 学校id
     * @return 楼宇列表数据
     */
    @GetMapping("/list/{schoolId}")
    public Result<List<BuildingVo>> getBuildingList(@PathVariable(value = "schoolId",required = false) Integer schoolId) {
        if (schoolId == null){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }

        return buildingsService.getBuildingList(schoolId);
    }

    /**
     * 新增楼宇
     * @param buildingPo 楼宇信息
     * @return 反馈
     */
    @PostMapping("/add")
    public Result<String> addBuilding(@RequestBody(required = false) BuildingPo buildingPo){
        if(buildingPo == null || buildingPo.getSchoolId() == null || buildingPo.getBno() == null || buildingPo.getBfn() == null || buildingPo.getDn() == null){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }

        return buildingsService.addBuilding(buildingPo);
    }

    /**
     * 分配管理员
     * @param adminId 管理员id
     * @param buildingId 楼宇id
     * @return 反馈
     */
    @PostMapping("/associate_admin/{adminId}/{buildingId}")
    public Result<String> associateAdmin(
            @PathVariable(value = "adminId", required = false)  Integer adminId,
            @PathVariable(value = "buildingId", required = false) Integer buildingId
    ){
        if(adminId == null || buildingId == null){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }

        return buildingsService.associateAdmin(adminId,buildingId);
    }

    /**
     * 获取楼宇详情
     * @param buildingId 楼宇id
     * @return 详情
     */
    @GetMapping("/details/{buildingId}")
    public Result<List<BuildingDetailsVo>> getBuildingDetails(@PathVariable(value = "buildingId",required = false) Integer buildingId){
        if(buildingId == null ){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }

        return buildingsService.getBuildingDetails(buildingId);
    }

    /**
     * 获取空余床位详情
     * @param schoolId 学校id
     * @return 详情
     */
    @GetMapping("/rest_bed_details/{schoolId}")
    public Result<RestBedDetails> getRestBedDetails(@PathVariable(value = "schoolId",required = false) Integer schoolId){
        if(schoolId == null ){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }

        return buildingsService.getRestBedDetails(schoolId);
    }


    /**
     * 一键分配宿舍
     * @param allocaDormitoryPo 宿舍分配信息
     * @return 反馈
     */
    @PostMapping("/allocation_dormitory_with_one_click")
    public Result<String> allocationDormitoryWithOneClick(@RequestBody(required = false)AllocaDormitoryPo allocaDormitoryPo){
        if(allocaDormitoryPo == null ){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }

        return buildingsService.allocationDormitoryWithOneClick(allocaDormitoryPo);
    }
}
